package com.gab.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gab.mapper.TbIntroductionMapper;
import com.gab.pojo.TbIntroduction;
import com.gab.service.TbIntroductionService;
import com.gab.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import  com.gab.pojo.TbIntroductionExample;
import  com.gab.pojo.TbIntroductionExample.Criteria;

@Service
public class TbIntroductionServiceImpl  implements TbIntroductionService{
	@Autowired
	private TbIntroductionMapper tbIntroductionMapper;
	
	public List<TbIntroduction> findAll() {
		// TODO Auto-generated method stub
		return tbIntroductionMapper.selectByExample(null);
	}

	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbIntroduction> page=   (Page<TbIntroduction>) tbIntroductionMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	public void add(TbIntroduction tbIntroduction) {
		tbIntroductionMapper.insert(tbIntroduction);
		
	}

	public void update(TbIntroduction tbIntroduction) {
		tbIntroductionMapper.updateByPrimaryKey(tbIntroduction);
		
	}

	public TbIntroduction findOne(Long id) {
		
		return tbIntroductionMapper.selectByPrimaryKey(id);
	}

	public void delete(Long[] ids) {
		for(Long id:ids){
			tbIntroductionMapper.deleteByPrimaryKey(id);
		}
		
	}

	public PageResult findPage(TbIntroduction tbIntroduction, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
		
		TbIntroductionExample example=new TbIntroductionExample();
		Criteria criteria = example.createCriteria();
		
		if(tbIntroduction!=null){			
			if(tbIntroduction.getContent()!=null && tbIntroduction.getContent().length()>0){
				criteria.andContentLike("%"+tbIntroduction.getContent()+"%");
			}
			if(tbIntroduction.getTitle()!=null && tbIntroduction.getTitle().length()>0){
				criteria.andTitleLike("%"+tbIntroduction.getTitle()+"%");
			}
			
	
		}
		
		Page<TbIntroduction> page= (Page<TbIntroduction>)tbIntroductionMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

}
